//---------------------------------------------------------------------------- // File: D3DManager.h // Class: D3DManager [v5.1] // Type: 3D API System Managment // Author: Ken Anderson // Date: 2/1/6 // OS dependant: Windows OS based on the fact that DirectX is windows only. // // Version: 2.0[12/24/04] -- Pipeline was removed and replaced with a direct // global CRSManager. // Version: 4.0[5/30/05] -- Removal of TP & VP function routines. // Version: 4.1[11/25/05] -- Replaced the handle to the D3DDevice with a pointer. // Version: 5.0[2/1/06] -- Updated to use the new vertex system that is based off // an array/table rather than a link list. // Version: 5.1[5/21/06] -- Updated to use references instead of pointers in certain // function instances as well as reducing any copy operations. // Notes: 3D System Manager that interfaces with directX to perform // rendering, adapters and other 3d routines. // // Required headers: // 1) C3DTypes.h -- Contains Common 3d types that are not specific to the renderer. // 2) D3DTypes.h -- Structures and definitions required for directX processing. // 3) OS_Global.h -- A link to the operating system which Direct3d requires. // 4) Banks3D.h -- The render banks required for the renderer. //---------------------------------------------------------------------------- #ifndef __D3DMANAGER__ #define __D3DMANAGER__ #include "OS_Global.h" #include "C3DTypes.h" #include "D3DTypes.h" #include "Banks3D.h" const int MAXADAPTERS =10; const int MAXMODES =200; const int MAXSTENCILDEPTHS =30; const DWORD INDEX32SUPPORT =0x0000FFFF; const BYTE byTexDepth = 2; typedef struct LAST_TRANSFORM_STATE { D3DMATRIX view; D3DMATRIX world; D3DMATRIX projection; } LTS; class D3DManager { private: //Win specific HWND m_hwnd; //OS Specific Rect m_rcWin; //DirectX Objects & Devices. public: LPDIRECT3D9 m_pD3D; //DirectX8 3D Object. LPDIRECT3DDEVICE9 m_pd3dDevice; //Pointer to the device. private: BOOL m_bDeviceValid; //Used for Protection packets. //Renderer Properties LTS m_LastTransform; //The last transform state. PSC3DViewport m_pViewport; //The viewport state if pure device is selected. //Adapter members. PADAPTER m_pAdapter; //List of adapters supported by the system. BYTE m_byAdapterNum; //Number of adapters present in the system. BYTE m_byCurrentAdapter; //The current adapter being used. //Member functions. public: ////////////////////// // INLINE FUNCTIONS // ////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: Constructor // Date: 11/15/3 // Type: Direct3d System Management. // Desc: Initializes member variables. // Parameters: None // Return value: None ////////////////////////////////////////////////////////////////////////////////////////////////////// D3DManager::D3DManager(){Init();} /////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: Destructor // Date: 11/15/3 // Type: Direct3d System Management. // Desc: Cleans up & destroys member variables. // Parameters: None // Return value: None ////////////////////////////////////////////////////////////////////////////////////////////////////// D3DManager::~D3DManager(){ManagerCleanup();} /////////////////////// // COMPLEX FUNCTIONS // /////////////////////// //Constructor & destructor of class. D3DManager(const D3DManager& manager); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------CLASS CONTROL MANAGEMENT------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR Create(void** HVWND); C3DERR Destroy(); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------DISPLAY MANAGEMENT------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// //C3DERR AutoSelectDisplay(Dword dwHeight=768L, Dword dwWidth=1024L, C3DFORMAT ColorDepthFormat=C3DFMT_32BIT_X8R8G8B8, bool bWindowed=true, C3DDEVTYPE devtype=C3DDEV_HARDWARE) {return C3DERR_NA;} C3DERR ChangeDisplay(Dword dwHeight, Dword dwWidth, C3DFORMAT ColorDepthFormat, C3DDEVTYPE devtype=C3DDEV_HARDWARE); C3DERR ChangeDisplay(Dword dwHeight, Dword dwWidth, C3DFORMAT ColorDepthFormat, bool bWindowed, C3DDEVTYPE devtype=C3DDEV_HARDWARE); int ReturnCurrModeNum(); C3DERR ReturnCurrModeInfo(Dword& rdwHeight, Dword& rdwWidth, C3DFORMAT& rColorDepth); int ReturnNumModes(); C3DERR ReturnModeInfo(Dword dwModeNum, Dword& rdwHeight, Dword& rdwWidth, C3DFORMAT& rColorDepth); C3DERR SetMode(Dword dwModeNum); bool IsIndex32Capable(); C3DERR Fullscreen(); C3DERR Fullscreen(bool bFullScreen); C3DERR ResizeDisplay(); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------PAINTING ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////// // INLINE FUNCTIONS // ////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: EndScene // Date: 12/10/03 // Desc: Calls the end of scene routine after all the rendering has been completed. // Parameters: None // Return value: //////////////////////////////////////////////////////////////////////////////////////////////////////// inline C3DERR EndScene(){if(m_pd3dDevice != NULL) m_pd3dDevice->EndScene(); return C3D_OK;} /////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: Clear // Date: 9/18/05 // Desc: Clears the direct3d buffer. // Parameters: None // Return value: //////////////////////////////////////////////////////////////////////////////////////////////////////// inline void Clear() {if(m_pd3dDevice != NULL) m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0L );} /////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: Present // Date: 12/10/03 // Desc: Presents the rendered scene to the screen. // Parameters: None // Return value: None //////////////////////////////////////////////////////////////////////////////////////////////////////// inline void Present(){if(m_pd3dDevice != NULL)m_pd3dDevice->Present(NULL, NULL, NULL, NULL);} /////////////////////// // COMPLEX FUNCTIONS // /////////////////////// C3DERR BeginScene(); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------UTILITY MANAGEMENT------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR GetDeviceCaps(C3DCAPS& rcaps); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------TRANSFORM ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR SetTransform(C3DTransformState TS, SC3DMatrix4& rMatrix); C3DERR GetTransform(C3DTransformState TS, SC3DMatrix4& rMatrix); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------VIEWPORT ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR SetViewport(SC3DViewport& rVP); C3DERR GetViewport(SC3DViewport& rVP); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------RENDER ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////// // INLINE FUNCTIONS // ////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: SetRenderState // Date: 11/9/04 // Desc: Sets the current render state based on the value. // Parameters: // 1) C3DRenderState r == The Render state. // 2) Dword v == The render state value. // Return value: C3DERR == An error code returned by the C3DManager, please review // C3DTypes.h for more information on each code status. //////////////////////////////////////////////////////////////////////////////////////////////////////// inline C3DERR SetRenderState(C3DRenderState r, Dword v) {D3DRENDERSTATETYPE s; if(!TranslateRenderState(s, r, v)) return C3DERR_STATENA; if(m_pd3dDevice != NULL) m_pd3dDevice->SetRenderState(s, v); return C3D_OK;} /////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: SetTextureStageState // Date: 11/9/04 // Desc: Sets the texture stage state, which manages the way the textures operate from providing color // filters that adjust the coloring of textures to alpha transparencies. // Parameters: // 1) Byte b == The stage of the Texture state. // 2) C3DTextureStageState s == Texture Stage State to set. // 3) Dword v == The texture stage state value. // Return value: C3DERR == An error code returned by the C3DManager, please review // C3DTypes.h for more information on each code status. //////////////////////////////////////////////////////////////////////////////////////////////////////// inline C3DERR SetTextureStageState(Byte b, C3DTextureStageState s, Dword v) {DWORD V;D3DTEXTURESTAGESTATETYPE t; if(!TranslateTextureStageState(t, s, V, v)) return C3DERR_STATENA; if(m_pd3dDevice!=NULL) m_pd3dDevice->SetTextureStageState(b, t, V); return C3D_OK;} /////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: ClearTexture // Date: 4/8/2005 // Desc: Clears the current texture from the renderer. // This is a tool that is best used on objects that are rendered with no textures // after objects that are. // Parameters: // 1) Byte b == The stage of the Texture state. // Return value: C3DERR == An error code returned by the C3DManager, please review // C3DTypes.h for more information on each code status. //////////////////////////////////////////////////////////////////////////////////////////////////////// inline C3DERR ClearTexture(Byte b){if(m_pd3dDevice != NULL) m_pd3dDevice->SetTexture(b, NULL);return C3D_OK;} /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------RENDER BANK ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR ProcessVertexBuffer(HRBE pVertSlot, Dword dwFilter=BSV_NS); C3DERR ProcessTextureBuffer(HRBE pTextSlot); C3DERR ProcessIndexBuffer(HRBE pIndxSlot); void DestroyVertexBuffer(HRBE hVertSlot); void DestroyTextureBuffer(HRBE hTextSlot); void DestroyIndexBuffer(HRBE hIndxSlot); private: /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------INTERNAL ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// VOID Init(); VOID ManagerCleanup(); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------ADPATER ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: PurgeStencils // Date: 6/11/6 // Desc: Purges the list of stencils as well as each stencil depth held within. // Parameters: // 1) PSTENCILS p == A pointer to a list of stencil formats. // 2) INT n == An integer that represents the number of stencils in the list. // Return value: None //////////////////////////////////////////////////////////////////////////////////////////////////////// inline VOID PurgeStencils(PSTENCILS p, INT n) {if(p==NULL) return;while((n--)>0) SDELETE(p[n].pStencilDepthFormats);SDELETE(p);} /////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: PurgeDevices // Date: 6/11/6 // Desc: Purges the device list. // Parameters: // 1) PDEVICE p == A pointer to list of devices. // 2) INT n == An integer that represents the number of devices in the list. // Return value: None //////////////////////////////////////////////////////////////////////////////////////////////////////// inline VOID PurgeDevices(PDEVICE p, INT n) {if(p==NULL) return;while((n--)>0){PurgeStencils(p[n].pStencils, p[n].byNumStencils);SDELETE(p[n].pModes);}} /////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: PurgeAdapters // Date: 6/11/6 // Desc: Purges the Adapter list. // Parameters: // 1) PADAPTER p == A pointer to list of adapters. // 2) INT n == An integer that represents the number of adapters in the list. // Return value: None //////////////////////////////////////////////////////////////////////////////////////////////////////// inline VOID PurgeAdapters(PADAPTER p, INT n) {if(p==NULL) return;while((n--)>0) PurgeDevices(p[n].devices, p[n].byNumDevices);SDELETE(p);} /////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: PurgeHardwareLists // Date: 11/18/3 // Type: Direct3d Adapter Management. // Desc: Cycles through all adapters, devices, stencils and modes to clear all memory allocated for each // component's list. // Parameters: None // Return value: None //////////////////////////////////////////////////////////////////////////////////////////////////////// inline VOID PurgeHardwareLists(){PurgeAdapters(m_pAdapter, m_byAdapterNum);} C3DERR BuildAdapterList(); C3DERR BuildDeviceList(PADAPTER pAdapter, UINT uCurrentAdapter); C3DERR BuildModeList(PDEVICE pDevice, UINT uCurrentAdapter); C3DERR LoadStencilDepthFormats(UINT uAdapter, D3DDEVTYPE devtype, D3DFORMAT d3dModeFormat, STENCILS* tmpStencilDepths); C3DERR CreateAdapterList(); C3DERR InitializePresentation(D3DPRESENT_PARAMETERS& rd3dapp); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // -------------------ENVIRONMENTAL ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR Initialize3dEnvironment(); C3DERR Invalidate3dEnvironment(); C3DERR Restore3dEnvironment(); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------DISPLAY MANAGEMENT------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR SelectDisplay(Dword dwWidth, Dword dwHeight, C3DFORMAT ColorDepthFormat=C3DFMT_32BIT_X8R8G8B8, bool bWindowed=true, C3DDEVTYPE devtype=C3DDEV_HARDWARE); C3DERR UpdateDisplay(); C3DERR AdjustWindowStyle(bool bWindowed); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------BANK ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// VOID CalcSAFVF(WORD wUsage, DWORD& rdwFVF, WORD& rwSize); void UnloadBanks(); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------VERTEX BANK ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR UploadVertexBank(); void UnloadVertexBank(C3DRenderBankStates rbsState=BSV_DESTROY); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------TEXTURE BANK ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR UploadTextureBank(); void UnloadTextureBank(C3DRenderBankStates rbsState = BSV_DESTROY); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------INDEX BANK ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR UploadIndexBank(); void UnloadIndexBank(C3DRenderBankStates rbsState = BSV_DESTROY); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // -------------RENDER STATE TRANSLATOR ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// bool TranslateRenderState(D3DRENDERSTATETYPE& rD3DRS, C3DRenderState C3DRS, Dword& rdwValue); bool TranslateBlendOperation(D3DBLEND& rD3DBL, C3DBlend C3DBL); bool TranslateCullOperation(D3DCULL& rD3DCULL, C3DCull C3DC); bool TranslateTextureStageState(D3DTEXTURESTAGESTATETYPE& rD3DTSS, C3DTextureStageState C3DTSS, Dword& rdwValue, Dword dwValue); bool TranslateTextureOperation(D3DTEXTUREOP& rD3DTOP, C3DTextureOP C3DTOP); bool TranslateTextureFilter(D3DTEXTUREFILTERTYPE& rD3DTFT, C3DTextureFilterType C3DTFT); bool TranslateTextureAddress(D3DTEXTUREADDRESS& rD3DTAD, C3DTextureAddressType C3DTAT); bool TranslateTextureArgument(Byte& rbArgument, C3DTextureArgumentType C3DTAT); bool TranslateCompareOperation(D3DCMPFUNC& rD3DCF, C3DCmpOp C3DCOP); bool TranslateDepthFormats(D3DFORMAT& rD3DFormat, C3DFORMAT C3DFormat); bool TranslateDepthFormats(C3DFORMAT& rC3DFormat, D3DFORMAT D3DFormat); bool TranslateFillMode(D3DFILLMODE& rD3Dfm, C3DFillMode C3Dfm); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------VERTEX BUFFER ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR CreateVertexBuffer(HRBE hVertSlot); C3DERR RefreshVertexBuffer(PSC3DVertices pVertices, LPDIRECT3DVERTEXBUFFER9 pVB, Word wSize=0x0000); //C3DERR DirectRenderVertexBuffer(PSC3DVertices pVertices, LPDIRECT3DVERTEXBUFFER9 pVB); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------INDEX BUFFER ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR CreateIndexBuffer(HRBE hIndSlot); C3DERR RefreshIndexBuffer(PSC3DIndices pIndices, LPDIRECT3DINDEXBUFFER9 pIB); C3DERR DirectRenderIndexVertBuffer(PSC3DVertices pVertices, LPDIRECT3DVERTEXBUFFER9 pVB, LPDIRECT3DINDEXBUFFER9 pIB); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ------------------TEXTURE BUFFER ROUTINES------------------------ // // ////////////////////////////////////////////////////////////////////////////////////////////////////// C3DERR CreateTextureBuffer(HRBE hTextSlot); LPDIRECT3DTEXTURE9 CreateTextureBufferFromFile(cstr strFileName); LPDIRECT3DTEXTURE9 CreateTextureBufferFromMemory(CiMedia* pMedia); C3DERR DirectRenderTextureBuffer(PSC3DTextures pTextures, LPDIRECT3DTEXTURE9 pTB); public: //Remove me. C3DERR UploadBanks(); }; #endif